package com.team.controller;

import com.team.entity.DevUser;
import com.team.entity.LoginCondition;
import com.team.service.DevUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;


@Controller
public class LoginController {
    @Autowired
    private DevUserService devUserService;
    @RequestMapping(value = "dologin")
    public String dologin(LoginCondition loginCondition, String VerificationCode,HttpSession httpSession, Model model){
        String inputCode = httpSession.getAttribute("codeValidate").toString();
        if (inputCode.equals(VerificationCode)){
            DevUser devUser1 = devUserService.loginSelect(loginCondition);

            if (devUser1==null){
                model.addAttribute("error","用户名或密码错误!");
                return "jsp/devlogin";
            } else{
                httpSession.setAttribute("devUser",devUser1);
                httpSession.setMaxInactiveInterval(4800);
                return "jsp/developer/main";
            }
        }else {
            model.addAttribute("error","验证码输入错误!");
            model.addAttribute("loginCondition",loginCondition);
            return "jsp/devlogin";
        }

    }

    //退出 就是清空session

    @RequestMapping(value = "/logout")
    public String Loginout(HttpSession httpSession){
        httpSession.removeAttribute("devUser");
       // httpSession.invalidate();//使整个窗口客户区无效
        return "redirect:jsp/devlogin.jsp";
    }

}
